
package com.hlkj.pay.controller.merchant;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.merchant.IMerchantDefaultConfigAppService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.merchant.DefaultFeeConfigQueryDto;
import com.hlkj.pay.dto.merchant.ProviderChannelFeeConfigList;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.model.merchant.DefaultFeeConfigDO;
import com.hlkj.pay.service.merchant.IMerchantDefaultConfigService;
import com.hlkj.pay.vo.ad.req.UpdateStatusReq;
import com.hlkj.pay.vo.common.req.DeleteIdReq;
import com.hlkj.pay.vo.merchant.req.DefaultFeeConfigPageQueryReq;
import com.hlkj.pay.vo.merchant.resp.DefaultFeeConfigResp;
import com.hlkj.pay.vo.merchant.resp.info.MerchantFeeConfigResp;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

/**
 * @author HlpayTeam
 * @date 2024/11/01 16:53
 */
@Tag(name = "默认费率信息")
@RestController
@RequestMapping("/v1/merchant/default")
@RequiredArgsConstructor
public class MerchantDefaultFeeConfigController extends BaseController {

    private final IMerchantDefaultConfigAppService merchantDefaultConfigAppService;

    private final IMerchantDefaultConfigService merchantDefaultConfigService;

    @Operation(summary = "默认费率列表")
    @GetMapping("/feeConfig/page")
    CommonResult<PageResult<DefaultFeeConfigResp>> queryProviderFeeConfig(DefaultFeeConfigPageQueryReq defaultFeeConfigPageQueryReq) {
        DefaultFeeConfigQueryDto defaultFeeConfigDO = BeanUtil.copyProperties(defaultFeeConfigPageQueryReq, DefaultFeeConfigQueryDto.class);
        PageResult<DefaultFeeConfigDO> page = merchantDefaultConfigService.queryPage(defaultFeeConfigPageQueryReq, defaultFeeConfigDO);
        List<DefaultFeeConfigDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        return buildPageResult(page, BeanUtil.copyToList(list, DefaultFeeConfigResp.class));
    }

    @Operation(summary = "删除默认费率配置")
    @DeleteMapping("/feeConfig")
    CommonResult<Void> delDefaultConfig(@Validated @RequestBody DeleteIdReq deleteIdReq) {
        return merchantDefaultConfigAppService.delDefaultConfig(deleteIdReq.getId());
    }

    @Operation(summary = "更新默认费率状态")
    @PostMapping("/feeConfig/status")
    CommonResult<Void> updateAdStatus(@Validated @RequestBody UpdateStatusReq updateStatusReq) {
        return merchantDefaultConfigAppService.updateAdStatus(updateStatusReq);
    }

    @Operation(summary = "获取默认费率配置")
    @GetMapping("/feeConfig/detail")
    CommonResult<List<MerchantFeeConfigResp>> queryDefaultFeeConfig(@RequestParam(name = "id",required = false) Long id) {
        return merchantDefaultConfigAppService.queryDefaultFeeConfig(id);
    }

    @Operation(summary = "保存默认费率配置")
    @PostMapping("/feeConfig")
    CommonResult<Void> saveDefaultFeeConfig(@RequestBody List<ProviderChannelFeeConfigList> providerChannelFeeConfigLists) {
        providerChannelFeeConfigLists = providerChannelFeeConfigLists.stream()
                .filter(providerChannelFeeConfigList -> CommonEnum.YES_NO_TYPE.YES.getCode().equals(providerChannelFeeConfigList.getStatus())).collect(
                        Collectors.toList());
        try {
            return merchantDefaultConfigAppService.saveDefaultFeeConfig(providerChannelFeeConfigLists);
        }catch (Exception e){
            return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(),e.getMessage());
        }
    }
}
